/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_client.h
 *
 * Purpose: implementation client side for cbus
 *
 * Developer:
 *   wen.gu , 2022-02-16
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __CBUS_CLIENT_H__
#define __CBUS_CLIENT_H__

#include <stdio.h>

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"
#include "cbus/bus/cbus_message.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/** use this arg, then don't care about response, just return after client send request*/
#define CBUS_CLIENT_NO_WAIT  (0)

/**use this arg, then do wait until receive response or the communication has terminate with exception */
#define CBUS_CLIENT_WAIT_FOREVER ((GU32)0xffffffff)

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START



struct _cbus_client;
typedef struct _cbus_client* cbus_client_t;


/**
 * @brief process the response of method call
 * @note  the message id maybe method id or property id and so on
 * 
 */
typedef void (*cbus_client_on_response_func)(cbus_message_t response_message);

/**
 * @brief a callback to handle the event info
 * @note  the message id is event id
 */
typedef void (*cbus_client_on_event_func)(cbus_message_t event);

typedef struct _cbus_client_event
{
    cbus_message_id event_id;
    cbus_client_on_event_func on_event;
    GPTR opaque;
}cbus_client_event_t;

typedef struct _cbus_client_property
{
    cbus_message_id property_id;
    cbus_client_on_event_func on_event;
    GPTR opaque;
}cbus_client_property_t;

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

G_API cbus_client_t cbus_client_create(const GSTR url, GU08 interface_version);
G_API void cbus_client_destroy(cbus_client_t client);


G_API cbus_message_t cbus_client_alloc_request_message(cbus_client_t client, cbus_message_type_t message_type, cbus_message_id message_id, GU32 payload_size);

G_API GErrc cbus_client_request(cbus_client_t client, cbus_message_t request_message, cbus_message_t* response_message, GU32 timeout_ms);
G_API GErrc cbus_client_request_async(cbus_client_t client, cbus_message_t request_message, cbus_client_on_response_func on_response, GPTR opaque);

G_API GErrc cbus_client_request_no_return(cbus_client_t client, cbus_message_t request_message);

G_API GErrc cbus_client_subscribe(cbus_client_t client, const cbus_client_event_t* event_info, GU32 timeout_ms);
G_API GErrc cbus_client_subscribe_group(cbus_client_t client, const cbus_client_event_t* group, GU32  group_size, GU32 timeout_ms);

G_API GErrc cbus_client_stop_subscribe(cbus_client_t client, cbus_message_id event_id, GU32 timeout_ms);
//G_API GErrc cbus_client_stop_subscribe_all(cbus_client_t client);

/** if not set cache size with this API , then the default cache size is: 1   */
G_API GErrc cbus_client_set_event_cache_size(cbus_client_t client, GU32 cache_size);
/** todo, is need add more API to custom different event with different cache size */

G_API GErrc cbus_client_property_get(cbus_client_t client, cbus_message_id property_id, cbus_message_t* response_message, GU32 timeout_ms);
G_API GErrc cbus_client_property_get_sync(cbus_client_t client, cbus_message_id property_id, cbus_client_on_response_func on_response, GPTR opaque);

G_API GErrc cbus_client_property_set(cbus_client_t client, cbus_message_t request_message, cbus_message_t* response_message, GU32 timeout_ms);
G_API GErrc cbus_client_property_set_sync(cbus_client_t client, cbus_message_t request_message, cbus_client_on_response_func on_response, GPTR opaque);

G_API GErrc cbus_client_property_subcscribe(cbus_client_t client, const cbus_client_property_t* property_info, GU32 timeout_ms);
G_API GErrc cbus_client_property_subcscribe_group(cbus_client_t client, const cbus_client_property_t* group, GU32 group_size, GU32 timeout_ms);
G_API GErrc cbus_client_property_stop_subscribe(cbus_client_t client, cbus_message_id property_id, GU32 timeout_ms);

G_C_HEADER_END



#endif /** !__CBUS_CLIENT_H__ */

